<script setup lang="js">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { userApi } from '@/api/request'
import { useRouter } from 'vue-router'

// ===== 状态管理 =====
const router = useRouter()

// 表单相关状态
const form = reactive({
  username: '',
  passward: '',
  confirm_passward: ''
})
const formRef = ref(null)
const isloading = ref(false)

// ===== 表单验证规则 =====
const rules = reactive({
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    {
      min: 3,
      max: 15,
      message: '用户名长度应在3到15个字符之间',
      trigger: 'blur'
    },
    {
      pattern: /^[\u4e00-\u9fa5a-zA-Z0-9]{3,15}$/,
      message: '用户名只能包含中文、字母和数字',
      trigger: 'blur'
    }
  ],
  passward: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    { min: 6, max: 16, message: '密码长度应在6到16个字符之间', trigger: 'blur' }
  ],
  confirm_passward: [
    { required: true, message: '确认密码不能为空', trigger: 'blur' },
    {
      min: 6,
      max: 16,
      message: '确认密码长度应在6到16个字符之间',
      trigger: 'blur'
    }
  ]
})

// ===== 表单配置 =====
const formProps = {
  model: form,
  rules: rules,
  ref: 'formRef',
  labelWidth: 'auto',
  labelPosition: 'top',
  size: 'large'
}

// ===== 方法定义 =====
// 注册处理
const onSubmit = async () => {
  if (!formRef.value) return

  isloading.value = true
  try {
    // 密码一致性检查
    if (form.passward !== form.confirm_passward) {
      ElMessage.error('两次密码输入不一致，请重新输入')
      return
    }

    // 表单验证
    await formRef.value.validate()

    // 发送注册请求
    const res = await userApi.register(form)
    if (res.data.code === 401) {
      ElMessage.error('用户名已存在，请重新输入')
      return
    }

    ElMessage.success('注册成功')
    router.push('/login')
  } catch (error) {
    console.error('注册失败:', error)
    if (error.message) {
      ElMessage.error('表单验证失败，请检查输入')
    } else {
      ElMessage.error('注册失败，请重试')
    }
  } finally {
    isloading.value = false
  }
}

// 跳转到登录页面
const onLogin = () => {
  router.push('/login')
}
</script>

<template>
  <div class="register-container">
    <div class="register-box">
      <el-form v-bind="formProps" ref="formRef">
        <h2 class="register-title">用户注册</h2>
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            :prefix-icon="User"
          />
        </el-form-item>
        <el-form-item label="密码" prop="passward">
          <el-input
            v-model="form.passward"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirm_passward">
          <el-input
            v-model="form.confirm_passward"
            type="password"
            placeholder="请再次输入密码"
            :prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="onSubmit"
            :loading="isloading"
            class="submit-btn"
            >注册</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="onLogin" class="login-btn" text
            >已有账号？返回登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.register-box {
  width: 400px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
  }
}

.register-title {
  text-align: center;
  color: #333;
  font-size: 24px;
  margin-bottom: 30px;
  font-weight: 600;
}

.submit-btn {
  width: 100%;
  height: 44px;
  font-size: 16px;
  margin-top: 20px;
  border-radius: 8px;
}

.login-btn {
  width: 100%;
  margin-top: 10px;
  color: #666;

  &:hover {
    color: #409eff;
  }
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #333;
}
</style>
